Ext.define('cloud.controller.Push', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.picker.Picker'],
    config: {
        refs: {
            push: 'push',
            pushmessage: 'pushmessage',
            push_submit: 'pushmessage #push_submit',
            btn_push: 'push #btn_push',
            pushallmessage: 'pushallmessage',
            push_all_submit: 'pushallmessage #push_all_submit'


        },
        control: {
            /**
             * search app
             */
            search_app: {
                change: function () {
                    var appStore = Ext.getStore('AppAllFindStore');
                    var value = this.getSearch_app().getValue();
                    appStore.clearFilter();
                    if (value != "") {
                        appStore.filter('name', value, true);
                    }
                    appStore.load();
                }
            },
            'push list': {
                itemtap: function (list, item, index, record, target, event) {
                    if (target.target.name == 'click_push') {
                        Ext.Viewport.mask({xtype: 'loadmask' });
                        var push = this.getPush();
                        var pushmessage = this.getPushmessage();
                        if (pushmessage == null) {
                            pushmessage = Ext.create('cloud.view.push.PushMessage');
                        }
                        pushmessage.setRecord(record);
                        push.push(pushmessage);
                        Ext.Viewport.unmask();
                    }
                }
            },
            push_submit: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var type = Ext.getCmp('push_type').getValue();
                    if (type == 'ios') {
                        this.getPushmessage().submit({
                            url: GLOBAL.api_url + 'push_ios_message',
                            params: {
                                appid: localStorage.getItem('caid')
                            },
                            scope: this,
                            method: 'POST',
                            success: this.executeSuccess,
                            failure: function () {
                                Ext.Msg.alert('推送失败');
                                Ext.Viewport.unmask();
                            }
                        });
                    }
                    if (type == 'android') {
                        this.getPushmessage().submit({
                            url: GLOBAL.api_url + 'jpush_android',
                            params: {
                                appid: localStorage.getItem('caid')
                            },
                            scope: this,
                            method: 'POST',
                            success: this.executeSuccess,
                            failure: function () {
                                Ext.Msg.alert('推送失败');
                                Ext.Viewport.unmask();
                            }
                        });
                    }
                    Ext.Viewport.unmask();
                }
            },
            btn_push: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask' });
                    var push = this.getPush();
                    var pushall = this.getPushallmessage();
                    if (pushall == null) {
                        pushall = Ext.create('cloud.view.push.PushAllMessage');
                    }

                    Ext.Ajax.request({
                        url: GLOBAL.api_url + 'push_type_find',
                        params: {caid: localStorage.getItem('caid')},
                        scope: this,
                        success: function (response) {
                            var result = Ext.decode(response.responseText);
                            if (result.Variables.Result.code == 0) {
                                var data = result.Variables.data;
                                var province = data.province;
                                //省份
                                var options_province= [];
                                options_province.push({text:"选择省份",value:'0'})
                                for (var i = 0; i < province.length; i++) {
                                    options_province.push({text:province[i],value:province[i]})
                                }
                                Ext.getCmp('push_province').setOptions(options_province);

                                //城市
                                var city = data.city;
                                var options_city= [];
                                options_city.push({text:"选择城市",value:'0'})
                                for (var i = 0; i < city.length; i++) {
                                    options_city.push({text:city[i],value:city[i]})
                                }
                                Ext.getCmp('push_city').setOptions(options_city);

                                //运营商
                                var operator = data.operator;
                                var options_operator= [];
                                options_operator.push({text:"选择运营商",value:'0'})
                                for (var i = 0; i < operator.length; i++) {
                                    options_operator.push({text:operator[i],value:operator[i]})
                                }
                                Ext.getCmp('push_operator').setOptions(options_operator);

                                //应用版本
                                var versions = data.versions;
                                var options_versions= [];
                                options_versions.push({text:"选择版本",value:'0'})
                                for (var i = 0; i < versions.length; i++) {
                                    options_versions.push({text:versions[i],value:versions[i]})
                                }
                                Ext.getCmp('push_versions').setOptions(options_versions);

                            }
                        }
                    });
                    push.push(pushall);
                    Ext.Viewport.unmask();
                }
            },
            push_all_submit:{
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                        this.getPushallmessage().submit({
                            url: GLOBAL.api_url + 'push_all_message',
                            params: {
                                appid: localStorage.getItem('caid')
                            },
                            scope: this,
                            method: 'POST'
//                            success: this.executeSuccess,
//                            failure: function () {
//                                Ext.Msg.alert('推送失败');
//                                Ext.Viewport.unmask();
//                            }
                        });
                    Ext.Msg.alert('推送成功');
                    Ext.getStore('Push').load({params: {caid: localStorage.getItem('caid')}});
                    this.getPush().pop();
                    Ext.Viewport.unmask();
                }
            }
        }
    },
    executeSuccess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            Ext.getStore('Push').load({params: {caid: localStorage.getItem('caid')}});
            this.getPush().pop();
//            Ext.Viewport.unmask();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
        Ext.Viewport.unmask();
    }
});